﻿using HandyControl.Controls;
using Microsoft.Win32;
using Ping9719.WpfEx.Mvvm;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Documents;
using System.Windows.Input;

namespace BaseUi.ViewModel
{
    public class SetViewModel : BindableBase
    {
        private List<int> SaveLoginCountData_ = new List<int> { 3, 5, 7, 10 };
        //记忆最大数量下拉框
        public List<int> SaveLoginCountData { get => SaveLoginCountData_; set { SetProperty(ref SaveLoginCountData_, value); } }

        private Dictionary<int, string> LogTimeData_ = new Dictionary<int, string>()
        {
            { 30,"一月"},{ 91,"三月"},{ 182,"半年"},{ 365,"一年"},{ 1095,"三年"},{ -1,"永久"},
        };
        //日志缓存时间下拉框
        public Dictionary<int, string> LogTimeData { get => LogTimeData_; set { SetProperty(ref LogTimeData_, value); } }


        #region 页面输入信息

        private bool Powerboot_;
        public bool Powerboot { get => Powerboot_; set { SetProperty(ref Powerboot_, value); } }

        private bool StartupFull_;
        public bool StartupFull { get => StartupFull_; set { SetProperty(ref StartupFull_, value); } }

        private bool IsSaveLogin_;
        public bool IsSaveLogin { get => IsSaveLogin_; set { SetProperty(ref IsSaveLogin_, value); } }

        private int SaveLoginCount_;
        public int SaveLoginCount { get => SaveLoginCount_; set { SetProperty(ref SaveLoginCount_, value); } }

        private int SaveLogDay_;
        public int SaveLogDay { get => SaveLogDay_; set { SetProperty(ref SaveLogDay_, value); } }
        private string Electric_;
        public string Electric { get => Electric_; set { SetProperty(ref Electric_, value); } }
        private string Change1_;
        public string Change1 { get => Change1_; set { SetProperty(ref Change1_, value); } }
        private string Change2_;
        public string Change2 { get => Change2_; set { SetProperty(ref Change2_, value); } }
        private string Vision1_;
        public string Vision1 { get => Vision1_; set { SetProperty(ref Vision1_, value); } }
        private string Vision2_;
        public string Vision2 { get => Vision2_; set { SetProperty(ref Vision2_, value); } }
        private string Text1_;
        public string Text1 { get => Text1_; set { SetProperty(ref Text1_, value); } }
        private string Text2_;
        public string Text2 { get => Text2_; set { SetProperty(ref Text2_, value); } }
        private string Text3_;
        public string Text3 { get => Text3_; set { SetProperty(ref Text3_, value); } }
        private string Text4_;
        public string Text4 { get => Text4_; set { SetProperty(ref Text4_, value); } }
        /// <summary>
        /// 安普板地址
        /// </summary>
        private string Amp1_;
        public string Amp1 { get => Amp1_; set { SetProperty(ref Amp1_, value); } }
        private string Amp2_;
        public string Amp2 { get => Amp2_; set { SetProperty(ref Amp2_, value); } }
        private string Amp3_;
        public string Amp3 { get => Amp3_; set { SetProperty(ref Amp3_, value); } }
        private string Amp4_;
        public string Amp4 { get => Amp4_; set { SetProperty(ref Amp4_, value); } }

        private bool IsAn_;

        public bool IsAn { get => IsAn_; set { SetProperty(ref IsAn_, value); } }


        /// <summary>
        /// 上下限
        /// </summary>
        private string DSensor1_;
        public string DSensor1 { get => DSensor1_; set { SetProperty(ref DSensor1_, value); } }
        private string USensor1_;
        public string USensor1 { get => USensor1_; set { SetProperty(ref USensor1_, value); } }

        private string DSensor2_;
        public string DSensor2 { get => DSensor2_; set { SetProperty(ref DSensor2_, value); } }
        private string USensor2_;
        public string USensor2 { get => USensor2_; set { SetProperty(ref USensor2_, value); } }

        private string DSensor3_;
        public string DSensor3 { get => DSensor3_; set { SetProperty(ref DSensor3_, value); } }
        private string USensor3_;
        public string USensor3 { get => USensor3_; set { SetProperty(ref USensor3_, value); } }

        private string DSensor4_;
        public string DSensor4 { get => DSensor4_; set { SetProperty(ref DSensor4_, value); } }
        private string USensor4_;
        public string USensor4 { get => USensor4_; set { SetProperty(ref USensor4_, value); } }
        private string DHZ_;
        public string DHZ { get => DHZ_; set { SetProperty(ref DHZ_, value); } }
        private string UHZ_;
        public string UHZ { get => UHZ_; set { SetProperty(ref UHZ_, value); } }
        private string DUA_;
        public string DUA { get => DUA_; set { SetProperty(ref DUA_, value); } }
        private string UUA_;
        public string UUA { get => UUA_; set { SetProperty(ref UUA_, value); } }
        private string DLCDCLK_;
        public string DLCDCLK { get => DLCDCLK_; set { SetProperty(ref DLCDCLK_, value); } }
        private string ULCDCLK_;
        public string ULCDCLK { get => ULCDCLK_; set { SetProperty(ref ULCDCLK_, value); } }
        private string DVCC_;
        public string DVCC { get => DVCC_; set { SetProperty(ref DVCC_, value); } }
        private string UVCC_;
        public string UVCC { get => UVCC_; set { SetProperty(ref UVCC_, value); } }
        private string DVDD_;
        public string DVDD { get => DVDD_; set { SetProperty(ref DVDD_, value); } }
        private string UVDD_;
        public string UVDD { get => UVDD_; set { SetProperty(ref UVDD_, value); } }
        private string DGND_;
        public string DGND { get => DGND_; set { SetProperty(ref DGND_, value); } }
        private string UGND_;
        public string UGND { get => UGND_; set { SetProperty(ref UGND_, value); } }
        private string DSW_;
        public string DSW { get => DSW_; set { SetProperty(ref DSW_, value); } }
        private string USW_;
        public string USW { get => USW_; set { SetProperty(ref USW_, value); } }
        private string DLCDDAT_;
        public string DLCDDAT { get => DLCDDAT_; set { SetProperty(ref DLCDDAT_, value); } }
        private string ULCDDAT_;
        public string ULCDDAT { get => ULCDDAT_; set { SetProperty(ref ULCDDAT_, value); } }
        private string DNON_;
        public string DNON { get => DNON_; set { SetProperty(ref DNON_, value); } }
        private string UNON_;
        public string UNON { get => UNON_; set { SetProperty(ref UNON_, value); } }
        private string DLCDSTB_;
        public string DLCDSTB { get => DLCDSTB_; set { SetProperty(ref DLCDSTB_, value); } }
        private string ULCDSTB_;
        public string ULCDSTB { get => ULCDSTB_; set { SetProperty(ref ULCDSTB_, value); } }
        #endregion

        /// <summary>
        /// 刷新界面
        /// </summary>
        public void Update()
        {
            try
            {
                LocalFile.UpdateConfig();
                Powerboot = LocalFile.Config.Sys.Powerboot;
                StartupFull = LocalFile.Config.Sys.StartupFull;
                IsSaveLogin = LocalFile.Config.Sys.IsSaveLogin;
                SaveLoginCount = LocalFile.Config.Sys.SaveLoginCount;
                SaveLogDay = LocalFile.Config.Sys.SaveLogDay;
                Electric = LocalFile.Config.Electric;
                Change1 = LocalFile.Config.Change1;
                Change2 = LocalFile.Config.Change2;
                Vision1 = LocalFile.Config.Vision1;
                Vision2 = LocalFile.Config.Vision2;
                Text1 = LocalFile.Config.Text1;
                Text2 = LocalFile.Config.Text2;
                Text3 = LocalFile.Config.Text3;
                Text4 = LocalFile.Config.Text4;
                IsAn = LocalFile.Config.IsAn;
                DSensor1 = LocalFile.Config.DSensor[0].ToString();
                DSensor2 = LocalFile.Config.DSensor[1].ToString();
                DSensor3 = LocalFile.Config.DSensor[2].ToString();
                DSensor4 = LocalFile.Config.DSensor[3].ToString();
                //DSensor = LocalFile.Config.DSensor.ToString();
                //USensor = LocalFile.Config.USensor.ToString();
                USensor1 = LocalFile.Config.USensor[0].ToString();
                USensor2 = LocalFile.Config.USensor[1].ToString();
                USensor3 = LocalFile.Config.USensor[2].ToString();
                USensor4 = LocalFile.Config.USensor[3].ToString();
                DHZ = LocalFile.Config.DHZ.ToString();
                UHZ = LocalFile.Config.UHZ.ToString();
                DUA = LocalFile.Config.DUA.ToString();
                UUA = LocalFile.Config.UUA.ToString();
                DLCDCLK = LocalFile.Config.DLCDCLK.ToString();
                ULCDCLK = LocalFile.Config.ULCDCLK.ToString();
                DVCC = LocalFile.Config.DVCC.ToString();
                UVCC = LocalFile.Config.UVCC.ToString();
                DVDD = LocalFile.Config.DVDD.ToString();
                UVDD = LocalFile.Config.UVDD.ToString();
                DGND = LocalFile.Config.DGND.ToString();
                UGND = LocalFile.Config.UGND.ToString();
                DSW = LocalFile.Config.DSW.ToString();
                USW = LocalFile.Config.USW.ToString();
                DLCDDAT = LocalFile.Config.DLCDDAT.ToString();
                ULCDDAT = LocalFile.Config.ULCDDAT.ToString();
                DNON = LocalFile.Config.DNON.ToString();
                UNON = LocalFile.Config.UNON.ToString();
                DLCDSTB = LocalFile.Config.DLCDSTB.ToString();
                ULCDSTB = LocalFile.Config.ULCDSTB.ToString();
                Amp1 = LocalFile.Config.Amp1.ToString();
                Amp2 = LocalFile.Config.Amp2.ToString();
                Amp3 = LocalFile.Config.Amp3.ToString();
                Amp4 = LocalFile.Config.Amp4.ToString();
            }
            catch (Exception ex)
            {
                Growl.Info($"获取配置失败。{ex.Message}");
            }
        }

        public ICommand SaveCommand { get => new DelegateCommand(Save); }
        /// <summary>
        /// 保存
        /// </summary>
        public void Save()
        {
            try
            {
                LocalFile.Config.Sys.Powerboot = Powerboot;
                LocalFile.Config.Sys.StartupFull = StartupFull;
                LocalFile.Config.Sys.IsSaveLogin = IsSaveLogin;
                LocalFile.Config.Sys.SaveLoginCount = SaveLoginCount;
                LocalFile.Config.Sys.SaveLogDay = SaveLogDay;
                LocalFile.Config.Electric = Electric;
                LocalFile.Config.Change1 = Change1;
                LocalFile.Config.Change2 = Change2;
                LocalFile.Config.Vision1 = Vision1;
                LocalFile.Config.Vision2 = Vision2;
                LocalFile.Config.Text1 = Text1;
                LocalFile.Config.Text2 = Text2;
                LocalFile.Config.Text3 = Text3;
                LocalFile.Config.Text4 = Text4;
                LocalFile.Config.IsAn = IsAn;
                LocalFile.Config.DSensor[0] = int.Parse(DSensor1);
                LocalFile.Config.DSensor[1] = int.Parse(DSensor2);
                LocalFile.Config.DSensor[2] = int.Parse(DSensor3);
                LocalFile.Config.DSensor[3] = int.Parse(DSensor4);
                LocalFile.Config.USensor[0] = int.Parse(USensor1);
                LocalFile.Config.USensor[1] = int.Parse(USensor2);
                LocalFile.Config.USensor[2] = int.Parse(USensor3);
                LocalFile.Config.USensor[3] = int.Parse(USensor4);
                LocalFile.Config.DHZ = int.Parse(DHZ);
                LocalFile.Config.UHZ = int.Parse(UHZ);
                LocalFile.Config.DUA = int.Parse(DUA);
                LocalFile.Config.UUA = int.Parse(UUA);
                LocalFile.Config.DLCDCLK = int.Parse(DLCDCLK);
                LocalFile.Config.ULCDCLK = int.Parse(ULCDCLK);
                LocalFile.Config.DVCC = int.Parse(DVCC);
                LocalFile.Config.UVCC = int.Parse(UVCC);
                LocalFile.Config.DVDD = int.Parse(DVDD);
                LocalFile.Config.UVDD = int.Parse(UVDD);
                LocalFile.Config.DGND = int.Parse(DGND);
                LocalFile.Config.UGND = int.Parse(UGND);
                LocalFile.Config.DSW = int.Parse(DSW);
                LocalFile.Config.USW = int.Parse(USW);
                LocalFile.Config.DLCDDAT = int.Parse(DLCDDAT);
                LocalFile.Config.ULCDDAT = int.Parse(ULCDDAT);
                LocalFile.Config.DNON = int.Parse(DNON);
                LocalFile.Config.UNON = int.Parse(UNON);
                LocalFile.Config.DLCDSTB = int.Parse(DLCDSTB);
                LocalFile.Config.ULCDSTB = int.Parse(ULCDSTB);
                LocalFile.Config.Amp1 = int.Parse(Amp1);
                LocalFile.Config.Amp2 = int.Parse(Amp2);
                LocalFile.Config.Amp3 = int.Parse(Amp3);
                LocalFile.Config.Amp4 = int.Parse(Amp4);
                LocalFile.SaveConfig();
                Growl.Success($"保存成功。");

                RunPowerboot(Powerboot);
            }
            catch (Exception ex)
            {
                LocalFile.UpdateConfig();
                Growl.Error($"保存失败。{ex.Message}");
            }
        }

        private void RunPowerboot(bool isPowerboot)
        {
            try
            {
                if (isPowerboot)
                {
                    RegistryKey rgkRun = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                    if (rgkRun == null)
                        rgkRun = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");

                    rgkRun.SetValue(Path.GetFileNameWithoutExtension(LocalFile.AppName), "\"" + Path.Combine(LocalFile.AppDir, LocalFile.AppName) + "\"");
                }
                else
                {
                    RegistryKey rgkRun = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                    if (rgkRun == null)
                        rgkRun = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");

                    rgkRun.DeleteValue(Path.GetFileNameWithoutExtension(LocalFile.AppName), false);
                }
            }
            catch (Exception ex)
            {
                Logs.Write(ex);
            }
        }

    }
}
